// src/pages/catering/exhibition/index.js
import getIn from '../../../../yali-sdk/utils/getIn';
import numberToWeekDay from '../../../../yali-sdk/utils/numberToWeekDay';
import { getImagePath, showFail, showSuccess } from '../../../config/common';
import { SUCCESS } from '../../../../yali-sdk/utils/success';
import { FAIL } from '../../../../yali-sdk/utils/fail';
import fetchShopDetail from '../../../../yali-sdk/business/shop/fetchShopDetail';
import goodsListQuery from '../../../../yali-sdk/business/catering/goodsListQuery';
import { CATERING_PRODUCT_DETAIL, POP_GOODS_LIST } from '../../../pages/pathUrl';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop_detail: {},
    product:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.multiShopList(options);
  },
  // 获取门店详情
  multiShopList(params) {
    const cate_id = getIn(params, ['cate_id']);
    const id = getIn(params, ['id']);
    wx.showNavigationBarLoading()
    global.ActionDispatcher.execute(fetchShopDetail, params, (code, res) => {
      let shop_detail = { id };
      if (code == 'SUCCESS') {
        wx.hideNavigationBarLoading()
        shop_detail = res.data;
        wx.setNavigationBarTitle({
          title: shop_detail.name
        });
        this.fetchData(shop_detail)
      } else if (code == 'FAIL') {
        console.log('调用失败')
      }
      this.setData({
        token: params.token,
        shop_detail,
        cate_id
      });
    });
  },
  // 访问列表数据
  fetchData(shop_detail) {
    const token = getIn(getApp(), ['globalData', 'token']);
    const params = { token, shop_id: shop_detail.id, is_index: true };
    global.ActionDispatcher.execute(goodsListQuery, params, (code, res) => {
      if (code == SUCCESS) {
        wx.hideLoading();
        const product = getIn(res.data, ['list']);
        this.setData({ product })
      } else if (code == FAIL) {
        wx.hideLoading();
      }
    });
  },
  // 拨打商家电话
  makePhone() {
    const { shop_detail } = this.data;
    wx.makePhoneCall({
      phoneNumber: shop_detail.tel
    })
  },
  // 跳转产品详情
  toDetail(e) {
    const id = getIn(e.currentTarget, ['id']);
    const { shop_detail } = this.data;
    const queryString = `id=${id}&color=#000&shop_id=${shop_detail.id}&template=${shop_detail.template}`;
    global.navigateHelper && global.navigateHelper.navigateTo({ pathname: CATERING_PRODUCT_DETAIL, queryString });
  },
  // 跳转产品列表页
  toGoodsList() {
    const queryString = `shop_detail=${JSON.stringify(this.data.shop_detail)}`;
    global.navigateHelper && global.navigateHelper.navigateTo({ pathname: POP_GOODS_LIST, queryString });
  },
})